package btracker;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Stores all data in user directory and gives that data to Classes.
 * Also puts modified data back into files found in the user directory
 *
 * @author Sammy
 * @version March 4 2010
 */
public class StoredData {

    ///////////////////
    ///DATA ARRAYS////
    //////////////////


    //declare arrays//
    static String MainDataBase[][];
    static String ReportData[][];
    static String Category[][];
    //declare variables//
    static String checkActive;
    static int UserBudget ;
    static int numSubCategory;
    static int numOfEntry;
    static int n3;
    static int i1;
    public static void getDataFromFiles() throws FileNotFoundException, IOException {


        ////////////////////////////
        ///Get Budget Set By User///
        ////////////////////////////

        System.out.println("-----------------------------------");
        System.out.println("Starting Read Sequence...");
        System.out.println("-----------------------------------");
        //declare Stream.IO readers
        FileReader UserData = new FileReader(Login.dir + "//UserData.data");
        BufferedReader readBudget = new BufferedReader(UserData);
        System.out.println("Starting UserBudget Read...");

        //Start Reading//
        readBudget.readLine(); //skip line
        readBudget.readLine(); //skip line
        readBudget.readLine(); //skip line]

        //Check if user account is new or active
        checkActive = readBudget.readLine();

        if(checkActive.equalsIgnoreCase("active")){

            System.out.println("User account is ACTIVE...");


        UserBudget = Integer.parseInt(readBudget.readLine());

        System.out.print("UserBudget Read and Stored. /");


        /////////////////////////////////
        ///Get Data From Main Database///
        /////////////////////////////////

      
        //declare Stream.IO readers
        FileReader DataBase = new FileReader(Login.dir + "//DataBase.db");
        BufferedReader readDataBase = new BufferedReader(DataBase);
        System.out.println("Starting Main Database Read...");

        //Find number of entries//
        numOfEntry = Integer.parseInt(readDataBase.readLine());
        for ( int i =0; i<numOfEntry; i++){
           for (int j =0; j<9; j++){
            MainDataBase[i][j] = readDataBase.readLine();
           }
        }
        System.out.print("Main DataBase Read and Stored. /");


        //////////////////////////
        ///Get Data From Report///
        //////////////////////////
       

        //declare Stream.IO readers
        FileReader ReportDataFile = new FileReader(Login.dir + "//ReportData.data");
        BufferedReader readReport = new BufferedReader(ReportDataFile);
        System.out.println("Starting ReportData Read...");
        
        
        for ( int i =0; i<3; i++){
           for (int j =0; j<22; j++){
            ReportData[i][j] = readReport.readLine();
           }
        }
        System.out.println("Report Data Read and Stored. /");

        
        ////////////////////////////
        ///Get Data From Category///
        ////////////////////////////


        //declare Stream.IO readers
        FileReader CategoryDataFile = new FileReader(Login.dir + "//ReportData.data");
        BufferedReader readCategory = new BufferedReader(ReportDataFile);
        System.out.println("Starting ReportData Read...");

       
        n3 =Integer.parseInt(readCategory.readLine());
        for ( int i =0; i<n3; i++){//|||||Read Categories||||
            Category[i][i] =readCategory.readLine(); //Name
            Category[i][i+1] = readCategory.readLine();//Position
           numSubCategory = Integer.parseInt(readCategory.readLine());
            for (int j =i; j<numSubCategory; j++){//||||Read Sub Categories|||||
            Category[i][j+1] = readReport.readLine();//Name
            Category[i][j+2] = readReport.readLine();//Position
           i1 = Integer.parseInt(readCategory.readLine());
            for (int a = j; a< i1 ;a++){
            Category[i][+1] = readReport.readLine();//Name
            Category[i][j+2] = readReport.readLine();//Position
            }
           }
        }
        System.out.println("Category Data Read and Stored. /");

    }else{
            System.out.println("User account is IN ACTIVE. /");
    }
    }
    public static void putDataToFiles() throws FileNotFoundException, IOException {



        ////////////////////////////
        ///put Budget Set By User///
        ////////////////////////////

        System.out.println("-----------------------------------");
        System.out.println("Starting Write Sequence...");
        System.out.println("-----------------------------------");
        //declare Stream.IO readers
        FileWriter UserData = new FileWriter(Login.dir + "//UserData.data",true);
        BufferedWriter writeBudget = new BufferedWriter(UserData);
        System.out.println("Starting UserBudget write...");

        //Start Reading//

        writeBudget.write(UserBudget);

        System.out.print("UserBudget Written to File. /");


        /////////////////////////////////
        ///Store Data to Main Database///
        /////////////////////////////////

       
        //declare Stream.IO readers
        FileWriter DataBase = new FileWriter(Login.dir + "//DataBase.db");
        BufferedWriter writeDataBase = new BufferedWriter(DataBase);
        System.out.println("Starting Main Database Write...");

        //Find number of entries//
         writeDataBase.write(Integer.toString(numOfEntry));
        for ( int i =0; i<numOfEntry; i++){
           for (int j =0; j<9; j++){
             writeDataBase.write(MainDataBase[i][j]);
           }
        }
        System.out.print("Main DataBase Written to File. /");


        //////////////////////////
        ///Store Data To Report///
        //////////////////////////


        //declare Stream.IO readers
        FileWriter ReportDataFile = new FileWriter(Login.dir + "//ReportData.data");
        BufferedWriter writeReport = new BufferedWriter(ReportDataFile);
        System.out.println("Starting ReportData Write...");


        for ( int i =0; i<3; i++){
           for (int j =0; j<22; j++){
              writeReport.write(ReportData[i][j]);
           }
        }
        System.out.println("Report Data Written to File. /");


        ////////////////////////////
        ///Store Data To Category///
        ////////////////////////////


        //declare Stream.IO readers
        FileWriter CategoryDataFile = new FileWriter(Login.dir + "//ReportData.data");
        BufferedWriter writeCategory = new BufferedWriter(ReportDataFile);
        System.out.println("Starting ReportData Write...");

       
         writeCategory.write(Integer.toString(n3));
        for ( int i =0; i<n3; i++){//|||||Read Categories||||
            writeCategory.write(Category[i][i]); //Name
            writeCategory.write(Category[i][i+1]);//Position
          writeCategory.write(Integer.toString(numSubCategory));
            for (int j =i; j<numSubCategory; j++){//||||Read Sub Categories|||||
             writeReport.write(Category[i][j+1]);//Name
             writeReport.write(Category[i][j+2]);//Position
           writeCategory.write(Integer.toString(i1));
            for (int a = j; a< i1 ;a++){
              writeReport.write(Category[i][+1]);//Name
              writeReport.write(Category[i][j+2]);//Position
            }
           }
        }
        System.out.println("Category Data Written to File. /");

    }
}
